<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:10
 */

namespace App\Ordersys\Controllers\Admin;

use App\Admin\Models\AuthGroupAccessModel;
use App\Admin\Models\MemberModel;
use App\Base\Contracts\AreaInterface;
use App\Base\Controllers\Controller;
use App\Ordersys\Contracts\OrdersysGoodsInterface;
use App\Ordersys\Contracts\OrdersysOrderInterface;
use App\Ordersys\Contracts\OrdersysPaytypeInterface;
use App\Ordersys\Contracts\OrdersysShippingInterface;
use App\Ordersys\Models\OrdersysCartModel;
use App\Ordersys\Models\WxuserModel;
use App\Ordersys\Models\OrdersysGoodsClassModel;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class OrderController extends Controller
{

    /**
     * 产品接口
     * @return OrdersysGoodsInterface
     */
    public function getOrdersysGoodsInterface()
    {
        return app()->make(OrdersysGoodsInterface::class);
    }


    /**
     * 请选择省区
     * @return AreaInterface
     */
    public function getAreaInterface(){
        return app()->make(AreaInterface::class);
    }

    /**
     * 快递
     * @return OrdersysShippingInterface
     */
    public function getOrdersysShippingInterface(){
      return app()->make(OrdersysShippingInterface::class);
    }

    /**
     * 支付方式
     * @return OrdersysPaytypeInterface
     */
    public function getOrdersysPaytypeInterface(){
        return app()->make(OrdersysPaytypeInterface::class);
    }
    /**
     * 订单接口
     * @return OrdersysOrderInterface
     */
    public function getOrdersysOrderInterface()
    {
        return app()->make(OrdersysOrderInterface::class);
    }

    /**
     * 获取订单购物车
     * @return OrdersysCartModel
     */
    public function getOrdersysCartModel(){
        return app()->make(OrdersysCartModel::class);
    }

    /**
 *
 * @return WxuserModel
 */
    public function getWxuserModel(){
        return app()->make(WxuserModel::class);
    }
    /**
     *
     * @return WxuserModel
     */
    public function getOrdersysGoodsClassModel(){
        return app()->make(OrdersysGoodsClassModel::class);
    }
    /**
     * 第一步选购商品
     */
    /**
     * 第一步选购商品
     */
    public function set_step1(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $goods_name = $request->input("goods_name");
                if (!empty($goods_name)) {
                    $query->where("goods_name", "like", '%' . $goods_name . '%');
                }
                $gc_name = $request->input("gc_name");
                if (!empty($gc_name)) {
                    $query->where("gc_name", $gc_name);
                }
            };
            return $this->getOrdersysGoodsInterface()->getList($where);
        }
        $where = function ($query) {
            $query->where("buyer_id", UID);
        };
        $cart_goods_id = $this->getOrdersysOrderInterface()->getCartIds($where);  //购物车产品ids
        //获取总金额
        $cart_all_money=$this->getOrdersysOrderInterface()->getCartAllPrice();
        //获取产品分类
        $gc_list= $this->getOrdersysGoodsClassModel()->newQuery()->get();

        return view("ordersys.order.set_step1")->with("cart_ids", $cart_goods_id)->with("cart_all_money",$cart_all_money)->with("gc_list",$gc_list);
    }

    /**
     * 更新购物车
     * @param Request $request
     */
    public function updateCart(Request $request){
        $num=$request->input("num");
        $goods_id=$request->input("goods_id");
        $model=$this->getOrdersysCartModel();
        $result=$model->editCart(["goods_num"=>$num],["goods_id"=>$goods_id,"buyer_id"=>UID]);
        if($result){
            return $model->cart_all_price;
        }else{
            return $model->cart_all_price;
        }
    }

    /**
     * 产生订单
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function set_step2(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            //获取购物车列表
            $cartList = $this->getOrdersysOrderInterface()->getCartList(["buyer_id" => UID]);
            return $cartList;
        }
        $where=function($query){
         $query->where("status",1);
        };
        $shipping_list=$this->getOrdersysShippingInterface()->getAllList($where);
        $where=function($query){
            $query->where("status",1);
        };
        $paytype_list=$this->getOrdersysPaytypeInterface()->getAllList($where); //支付方式
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域

        if (IS_ROOT) {
            $where = array();
            $where[$this->getAuthGroupAccessModel()->table.".group_id"] = 1;
            $memberList= $this->getAuthGroupAccessModel()->newQuery()
                ->select([$this->getAuthGroupAccessModel()->table.".uid",$this->getMemberModel()->table.".nickname"])
                ->leftJoin($this->getMemberModel()->table, $this->getAuthGroupAccessModel()->table . '.uid', '=', $this->getMemberModel()->table . '.uid')->where($where)->get();
        } else {
            $memberList =$this->getMemberModel()->select(["uid","nickname"])->where(array("uid" => UID))->get();
        }
        $all_price=$request->input("all_price");



        return view("ordersys.order.set_step2")->with("all_price",$all_price)->with("memberList",$memberList)->with("mid",UID)->with("areaList",$areaList)->with("shippingList",$shipping_list)->with("paytypeList",$paytype_list);
    }

    /**
     * 订单保存
     * @param Request $request
     * @return mixed
     */
    public function saveOrderData(Request $request){

        $validator = Validator::make($request->all(), [
            'wx_no' => 'required',
			'pay_type' => 'required',
            'express_type' => 'required',
        ], ["wx_no.required" => '微信号是必须的',"pay_type.required" => '支付类型是必须的',"express_type.required" => '快递类型是必须的']);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
       if($orderInfo=$this->getOrdersysOrderInterface()->saveData($request->all())){
           $data=$request->all();
           $data["order_id"]=$orderInfo->id;
           $this->getOrdersysOrderInterface()->saveOrderExpresData($data);
              //将购物车加到订单产品中去掉购物内容
           return  $this->getOrdersysOrderInterface()->saveOrderGoodsData($orderInfo->id);
       }else{
           throw new \Exception("操作失败");
       }
    }


    /**
     * 加入购物车
     * @param Request $request
     */
    public function addCart(Request $request)
    {
        if ($this->getOrdersysOrderInterface()->addCart($request->all())) {
            return array();
        } else {
            throw new \Exception("操作失败");
        }

    }

    /**
     * 清除购物车
     * @param Request $request
     */
    public function clearCart(Request $request)
    {
        $where = array();
        $where['buyer_id'] = UID;
        $where['goods_id'] = $request->input("id");
        $this->getOrdersysOrderInterface()->clearCart($where);
    }

    /**
     * 获取购物车价格
     * @return mixed
     */
    public function getCartAllPrice()
    {
        return $this->getOrdersysOrderInterface()->getCartAllPrice();
    }

    /**
     * @return AuthGroupAccessModel
     */
   public function getAuthGroupAccessModel(){
        return app()->make(AuthGroupAccessModel::class);
   }

    /**
     * 用户
     * @return MemberModel
     */
   public function getMemberModel(){
       return app()->make(MemberModel::class);
   }
    /**
     * vip新增
     */
    public function vipAdd(Request $request){

        if($request->getMethod() == 'POST'){
            $validator = Validator::make($request->all(), [
                'wx_no' => 'required',
            ], ["wx_no.required" => '微信号是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $data=$request->all();
            $data["order_type"]=2;
            $data["paid_amount"]=$request->input("all_amount");
            $data["pt_name"]=1;
            if($orderInfo=$this->getOrdersysOrderInterface()->saveData($data)){
                //将购物车加到订单产品中去掉购物内容
               return $orderInfo;
            }else{
                throw new \Exception("操作失败");
            }

        }
        if (IS_ROOT) {
            $where = array();
            $where[$this->getAuthGroupAccessModel()->table.".group_id"] = 1;
            $memberList= $this->getAuthGroupAccessModel()->newQuery()
                ->select([$this->getAuthGroupAccessModel()->table.".uid",$this->getMemberModel()->table.".nickname"])
                ->leftJoin($this->getMemberModel()->table, $this->getAuthGroupAccessModel()->table . '.uid', '=', $this->getMemberModel()->table . '.uid')->where($where)->get();
        } else {
            $memberList =$this->getMemberModel()->select(["uid","nickname"])->where(array("uid" => UID))->get();
        }

        $where=function($query){
            $query->where("status",1);
        };
        $paytype_list=$this->getOrdersysPaytypeInterface()->getAllList($where); //支付方式
       return view("ordersys.order.vip_add")->with("memberList",$memberList)->with("mid",UID)->with("paytypeList",$paytype_list);
    }

    public function vipLike(Request $request){
        $term=$request->input("term");
        $where=function($query) use ($term){
            $query->where("wx_no","like",'%'.$term.'%');

        };
        $list= $this->getWxuserModel()->newQuery()->where($where)->get();
        $data = array();
        foreach ($list as $key => $val) {
            $data[$key]["id"] = $val->wx_no;
            $data[$key]["value"] = $val->wx_no . " - " . $val->name;
            $data[$key]["name"] = $val->name;
        }
        echo json_encode($data);
        exit;
    }


}
